﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using SqlSugar.IOC;

namespace WPFNote.Repository
{
    public class RepositoryBasic<T, TS> : SimpleClient<T>, IDal.IRepositoryBasic<T, TS>
            where T : Models.BaseEntity, new() where TS : Models.BaseEntitySch, new()
    {
        public RepositoryBasic(ISqlSugarClient? context = null) : base(context)
        {
            var configId = typeof(T).GetCustomAttribute<TenantAttribute>()?.configId;
            if (configId == null)
            {
                base.Context = DbScoped.SugarScope;
            }
            else
            {
                base.Context = DbScoped.SugarScope.GetConnection(configId);
            }
        }


        /// <summary>
        /// 写入
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public override async Task<bool> InsertAsync(T t)
        {
            return await base.InsertAsync(t);
        }

        /// <summary>
        /// 批量写入
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(T[] t)
        {
            return await base.InsertRangeAsync(t);
        }

        /// <summary>
        /// 写入并返回ID
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> InsertReturnIdAsync(T t)
        {
            return await base.InsertReturnIdentityAsync(t);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public override async Task<bool> UpdateAsync(T t)
        {
            return await base.Context.Updateable(t).IgnoreColumns(ignoreAllNullColumns: true)
                .WhereColumns(it => new { it.Id }).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T t, TS ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Updateable(t).IgnoreColumns(ignoreAllNullColumns: true)
                .Where(sql, sugarParameters).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T[] t)
        {
            return await base.Context.Updateable(t).IgnoreColumns(ignoreAllNullColumns: true)
                .WhereColumns(it => new { it.Id }).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(T[] t, TS ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Updateable(t).IgnoreColumns(ignoreAllNullColumns: true)
                .Where(sql, sugarParameters).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await base.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="t"></param>
        /// <param name="ts"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t, TS ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Deleteable<T>().Where(sql, sugarParameters).ExecuteCommandAsync();
        }

        /// <summary>
        /// 多个删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(dynamic[] ids)
        {
            return await base.DeleteByIdsAsync(ids);
        }

        /// <summary>
        /// 根据ID查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<T> SelectAsync(int id)
        {
            return await base.GetByIdAsync(id);
        }

        /// <summary>
        /// 自定义条件单个查询
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public virtual async Task<T> SelectAsync(TS ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return (await base.Context.Queryable<T>()
                .OrderBy(ts?.OrderBy)
                .Where(sql, sugarParameters).ToListAsync()).FirstOrDefault();
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> SelectListAsync()
        {
            return await base.GetListAsync();
        }

        /// <summary>
        /// 自定义条件查询列表
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> SelectListAsync(TS ts)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Queryable<T>()
                .OrderBy(ts?.OrderBy)
                .Where(sql, sugarParameters).ToListAsync();
        }

        /// <summary>
        /// 分页查询全部数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> SelectListAsync(int page, int size, RefAsync<int> total)
        {
            return await base.Context.Queryable<T>().OrderBy(st => "Id", OrderByType.Desc)
                .ToPageListAsync(page, size, total);
        }

        /// <summary>
        /// 自定义查询条件分页查询列表
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public virtual async Task<List<T>> SelectListAsync(TS ts, int page, int size, RefAsync<int> total)
        {
            var (sql, sugarParameters) = Models.Utility.MySqlWhere.Sql(ts);
            return await base.Context.Queryable<T>().Where(sql, sugarParameters)
                .OrderBy(ts?.OrderBy)
                .ToPageListAsync(page, size, total);
        }
    }
}
